package com.imy.alscript;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.client.ipc.VActivityManager;
import com.imy.virtual.helper.utils.LaunchIntentUtils;
import com.imy.virtual.helper.utils.VLog;

public class LoadingActivity  extends Activity {
  private static final String PKG_NAME_ARGUMENT = "MODEL_ARGUMENT";
  private static final String KEY_INTENT = "KEY_INTENT";
  private static final String KEY_USER = "KEY_USER";
  public static void launch(Context context, String packageName, int userId) {
    Intent intent = LaunchIntentUtils.getLaunchIntent(packageName,userId);
    Intent loadingPageIntent = new Intent(context, LoadingActivity.class);
    loadingPageIntent.putExtra(PKG_NAME_ARGUMENT, packageName);
    loadingPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    loadingPageIntent.putExtra(KEY_INTENT, intent);
    loadingPageIntent.putExtra(KEY_USER, userId);
    try {
      VirtualCore.get().preOpt(packageName);
    }catch (Exception e){
     VLog.e("preOpt error:");
    }
    context.startActivity(loadingPageIntent);
  }
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loading);
    TextView tv =findViewById(R.id.package_tv);
    String stringExtra = getIntent().getStringExtra(PKG_NAME_ARGUMENT);
    tv.setText("即将启动App 包名:"+stringExtra);
    //执行
    Intent intent = getIntent().getParcelableExtra(KEY_INTENT);
    Log.i("LoadingActivity","intent:"+intent);
    if (intent==null){

      return;
    }
    finish();
    VActivityManager.get().startActivity(intent,0);
  }
}
